package SEApp
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class se_eventManager extends EventDispatcher {
		private static var _instance	: se_eventManager;	
		private var _app				: se_sapp;
		private var _waitList			: Array;
		
		public static function instance(): se_eventManager {
			return _instance;
		}
	
		public function se_eventManager(a_app: se_sapp, target:IEventDispatcher=null) {
			super(target);
			_instance	= this;
			_waitList	= new Array();
			_app = a_app;
			_app.addEventListener(se_sapp.STARTSTATE, doStartState);
		}		
		
		public function waitEvent(event:Event):Boolean {
			if (!_app.isStartComplete) {
				_waitList.push(event);
				return true;
			}
			
			return dispatchEvent(event);
		}
		
		public function get app(): se_app {
			return _app;
		}
		
		protected function doStartState(e: Event): void {
			_waitList.forEach(function(item: Event, index: int, arr: Array): void {
				dispatchEvent(item);
			});
			_waitList.splice(0, _waitList.length);
		}
	}
}